package mycollections;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class MyIterator<T> implements Iterator<T> {

	private MyList<T> collection;
	private int position;
	private int lastRet = -1;
	
	public MyIterator(MyList<T> collection) {
		this.collection = collection;
	}
	
	public boolean hasNext() {
		return position != collection.size();
	}
	
	public T next() {
		if(position >= collection.size())
			throw new NoSuchElementException();				
		return (T) collection.get(lastRet = position++);
	}
	
	public void remove() {
		System.out.println("lr: " + lastRet);
		if(lastRet == -1)
			throw new IllegalStateException();		
		collection.remove(lastRet);		
		lastRet = -1;
	}
	
}
